//
//  ScenicStoryNavBar.swift
//  LeTu
//
//  Created by g1 on 2024/1/13.
//

import UIKit

class ScenicStoryNavBar: UIView {

    var backHandle: (()->Void)?
    var languageHandle: (()->Void)?
    
    init(title: String) {
        super.init(frame: .zero)
        
        configUI()
        titleLabel.text = title
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        DispatchQueue.main.async {
            self.languageBtn.imagePosition(style: .right, spacing: 1)
        }
    }
    func setTitle(_ title: String) {
        titleLabel.text = title
    }
    func updateInfo() {
        languageBtn.setTitle(LanguageManager.shared.currentLanguage == .en ? "英文".localized:"中文".localized)
    }
    
    // MARK: - UIButtonAction
    @objc func backBtnAction() {
        guard let handle = backHandle else { return }
        handle()
    }
    @objc func languageBtnAction() {
        guard let handle = languageHandle else { return }
        handle()
    }
    
    // MARK: - configUI
    func configUI() {
        
        addSubview(backView)
        addSubview(defaultBarView)
        defaultBarView.addSubview(backBtn)
        defaultBarView.addSubview(languageBtn)
        defaultBarView.addSubview(titleLabel)
        
        let barHeight = 32+16
        backView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        defaultBarView.snp.makeConstraints { make in
            make.top.equalTo(Size.statusBarHeight)
            make.height.equalTo(barHeight)
            make.left.right.equalToSuperview()
        }
        backBtn.snp.makeConstraints { make in
            make.left.centerY.height.equalToSuperview()
            make.width.equalTo(barHeight)
        }
        languageBtn.snp.makeConstraints { make in
            make.right.equalTo(-12)
            make.top.bottom.equalToSuperview()
        }
        titleLabel.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.lessThanOrEqualTo(screenWidth/2)
        }
        
        backBtn.addTarget(self, action: #selector(backBtnAction))
        languageBtn.addTarget(self, action: #selector(languageBtnAction))
        
    }
    private lazy var backView: UIView = {
        let backView = UIView()
        backView.backgroundColor = .app_viewBackgroundColor
        backView.alpha = 0.0
        return backView
    }()
    private lazy var defaultBarView: UIView = {
        let backView = UIView()
        return backView
    }()
    private lazy var backBtn: CustomClickableView = {
        let backBtn = CustomClickableView()
        backBtn.setImage(UIImage(named: "back_white"))
        backBtn.setImageEdgeInsets(UIEdgeInsets(top: 8, left: 16, bottom: 8, right: 0))
        return backBtn
    }()
    private lazy var languageBtn: CustomClickableView = {
        let languageBtn = CustomClickableView()
        languageBtn.setFont(kSetPingFangMedium(14))
        languageBtn.setTitleColor(.app_white)
        languageBtn.setImage(UIImage(named: "home_down_gray_arrow"))
        return languageBtn
    }()
    private lazy var titleLabel: UILabel = {
        let titleLabel = UILabel()
        titleLabel.font = kSetPingFangRegular(17)
        titleLabel.textAlignment = .center
        titleLabel.textColor = .app_white
        return titleLabel
    }()
        
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
